package com.cmas.platform.controller;

import java.util.*;


import com.aidex.common.annotation.DataSource;
import com.aidex.common.annotation.Log;

import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.TableDataInfo;


import com.aidex.common.enums.BusinessType;
import com.aidex.common.enums.DataSourceType;

import com.aidex.common.utils.poi.ExcelUtil;
import com.aidex.common.core.controller.BaseController;
import com.aidex.framework.cache.DictUtils;



import com.cmas.platform.domain.ApproveRecord;
import com.cmas.platform.service.PostFullService;
import com.cmas.platform.vo.PostVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmas.platform.domain.Post;
import com.cmas.platform.service.PostService;


/**
 * 帖子管理Controller
 *
 * @author su
 * @email 1218819874@qq.com
 * @date 2023-03-15
 */
@DataSource(value = DataSourceType.SLAVE)
@RestController
@RequestMapping("/platform/post")
public class PostController extends BaseController {

    @Autowired
    private PostService postService;
    @Autowired
    private PostFullService postFullService;

    /**
     * 查询帖子管理列表
     */
    @PreAuthorize("@ss.hasPermi('platform:post:list')")
    @GetMapping("/list")
    public R<TableDataInfo> list(Post post) {
        List<PostVo> list = postService.selectPostVoList(post);
        return R.data(getDataTable(list));
    }


    /**
     * 获取帖子管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:post:query')")
    @GetMapping(value = "/{id}")
    public R<Post> detail(@PathVariable("id") String id) {
        return R.data(postService.getById(id));
    }


    /**
     * 审核帖子
     */
    @PreAuthorize("@ss.hasPermi('platform:post:add')")
    @Log(title = "审核帖子", businessType = BusinessType.INSERT)
    @PostMapping("/approve")
    public R approve(@RequestBody @Validated ApproveRecord approveRecord) {
        postService.approve(approveRecord);
        return R.success("审核成功");
    }

    /**
     * 新增帖子管理
     */
    @PreAuthorize("@ss.hasPermi('platform:post:add')")
    @Log(title = "帖子管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated Post post) {
        return postService.addPost(post);
    }

    /**
     * 修改帖子管理
     */
    @PreAuthorize("@ss.hasPermi('platform:post:edit')")
    @Log(title = "帖子管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated Post post) {
        return postService.updatePost(post);
    }

    /**
     * 删除帖子管理
     */
    @PreAuthorize("@ss.hasPermi('platform:post:remove')")
    @Log(title = "帖子管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids) {
        return R.data(postService.deletePostByIds(ids));
    }


    /**
     * 导出帖子管理列表
     */
    @PreAuthorize("@ss.hasPermi('platform:post:export')")
    @Log(title = "帖子管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(Post post) {
        List<Post> list = postService.selectPostList(post);
        ExcelUtil<Post> util = new ExcelUtil<Post>(Post.class);
        return util.exportExcel(list, "帖子管理数据");
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }


    // ***用户系统位置戳****

    /**
     * 查询帖子列表
     */
    @GetMapping("/listPostDto")
    public R<?> listPostDto(Post post,
                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.data(postFullService.getPostFullDto(pageNo, pageSize, post));
    }

    /**
     *  获取帖子
     * @param post
     * @return
     */
    @GetMapping("/getPostList")
    public R<?> getPostList(Post post,
                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.data(postFullService.selectPostFullDto(pageNo,pageSize,post));
    }



    @GetMapping("/selectKeyWordPostList")
    public R<?> selectKeyWordPostList(Post post,
                            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return R.data(postFullService.selectKeyWordPostFullDto(pageNo,pageSize,post));
    }



    /**
     * 获取帖子管理详细信息
     */
    @GetMapping(value = "/postDetail")
    public R<Post> postDetail(String id) {
        return R.data(postFullService.getPostDetail(id));
    }


    /**
     * 新增帖子
     */
    @Log(title = "帖子管理", businessType = BusinessType.INSERT)
    @PostMapping("/publish")
    public R publish(@RequestBody @Validated Post post) {
        post.setPublishTime(new Date());
        return postService.addPost(post);
    }


    /**
     * 修改帖子
     */
    @Log(title = "帖子管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public R update(@RequestBody @Validated Post post) {
        return postService.updatePost(post);
    }


    /**
     * 删除帖子管理
     */
    @Log(title = "帖子管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete")
    public R delete(String[] ids) {
        return R.data(postService.deletePostByIds(ids));
    }

}
